﻿using GrapeCity.Forguncy.CellTypes;
using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Vant.Properties;
using Vant.WpfControls.DrawingObject;

namespace Vant.CellTypes
{
    public class CalendarDesigner : CellTypeDesigner<Calendar>
    {
        public override FrameworkElement GetDrawingControl(ICellInfo cellInfo, IDrawingHelper drawingHelper)
        {
            return new CalendarDrawingObject(this.CellType, drawingHelper);
        }
    }

    [Designer("Vant.CellTypes.CalendarDesigner, Vant")]
    [Icon("pack://application:,,,/Vant;component/Resources/Images/Calendar.png")]
    [OrderWeight((int)Utils.OrderWeight.Calendar)]
    public class Calendar : VantCellTypeBase, ISupportDefaultValue, ISupportReadOnly, ISupportUIPermission
    {
        [SRDisplayName(nameof(Resources.Edit_ClickCommand))]
        [SRCustomCommandObject(InitParamProperties = "date", InitParamValues = nameof(Resources.Calendar_EditCommand_InitParamValues))]
        public object ClickCommand { get; set; }

        List<UIPermission> _uIPermissions;
        [SRDisplayName(nameof(Resources.Vant_UIPermissions))]
        [JsonProperty(ObjectCreationHandling = ObjectCreationHandling.Replace)]
        public List<UIPermission> UIPermissions
        {
            get
            {
                if (_uIPermissions == null)
                {
                    _uIPermissions = Utils.GetDefaultPermission(UIPermissionScope.Visible, UIPermissionScope.Editable);
                }
                return _uIPermissions;
            }
            set
            {
                _uIPermissions = value;
            }
        }

        [SRDisplayName(nameof(Resources.Vant_Type))]
        [SRComboProperty(ValueList = "single|multiple|range", DisplayList = nameof(Resources.Calendar_TypesDisplayList))]
        public string type { get; set; } = "single";

        [FormulaProperty]
        [SRDisplayName(nameof(Resources.Vant_DefaultValue))]
        public object DefaultValue { get; set; }

        [Browsable(false)]
        [JsonIgnore]
        public bool NeedFormatDefaultValue => true;

        [FormulaProperty]
        [SRDisplayName(nameof(Resources.Vant_MinDate))]
        public object minDate { get; set; }

        [FormulaProperty]
        [SRDisplayName(nameof(Resources.Vant_MaxDate))]
        public object maxDate { get; set; }

        [IntProperty(AllowNull = true, Min = 1)]
        [DefaultValue(null)]
        [SRDisplayName(nameof(Resources.Calendar_MaxRange))]
        public int? maxRange { get; set; }

        [BoolProperty(IndentLevel = 1)]
        [DefaultValue(false)]
        [SRDisplayName(nameof(Resources.Calendar_ShowRangePrompt))]
        public bool showRangePrompt { get; set; } = false;

        [SRDisplayName(nameof(Resources.Calendar_FirstDayOfWeek))]
        [SRComboProperty(ValueList = "0|1|2|3|4|5|6", DisplayList = nameof(Resources.Calendar_FirstDayOfWeek_DisplayList))]
        public string firstDayOfWeek { get; set; } = "0";

        [SRDisplayName(nameof(Resources.Vant_ThemeColor))]
        [ColorProperty]
        public string color { get; set; } = "Accent 1";

        [SRCategoryHeader(nameof(Resources.Other))]
        [SRDisplayName(nameof(Resources.Calendar_ShowMark))]
        [DefaultValue(true)]
        public bool showMark { get; set; } = true;

        [SRDisplayName(nameof(Resources.Calendar_ShowSubtitle))]
        [DefaultValue(true)]
        public bool showSubtitle { get; set; } = true;

        [SRDisplayName(nameof(Resources.Calendar_AllowSameDay))]
        [DefaultValue(false)]
        public bool allowSameDay { get; set; } = false;

        [SRDisplayName(nameof(Resources.Vant_Readonly))]
        [DefaultValue(false)]
        public bool ReadOnly { get; set; } = false;

        public override SupportFeatures SupportFeatures
        {
            get
            {
                return SupportFeatures.ShouldCheckDirtyWhenLeavePage;
            }
        }

        public override bool GetDesignerPropertyVisible(string propertyName)
        {
            if (propertyName == nameof(maxRange))
            {
                return type == "multiple" || type == "range";
            }
            if (propertyName == nameof(showRangePrompt))
            {
                return maxRange != null && type == "range";
            }
            if (propertyName == nameof(allowSameDay))
            {
                return type == "range";
            }
            return base.GetDesignerPropertyVisible(propertyName);
        }

        public override string ToString()
        {
            return Resources.Calendar_DisplayName;
        }
    }
}
